<template>
  <div>
    <a-form-model layout="inline">
      <a-row>
        <a-col style="float: left"
          ><a-button :style="{ marginBottom: '1rem' }" type="primary" @click="handleAddPatient"> 新增</a-button
          ><a-button :style="{ marginBottom: '1rem' }" type="primary" @click="handleBatchDelete"> 删除</a-button></a-col
        >
        <a-col style="float: right" class="searchClass">
          <a-form-model-item label="医生姓名">
            <a-input v-model="doctorsName" />
          </a-form-model-item>
          <a-form-model-item label="手机号">
            <a-input v-model="doctorsMobile" />
          </a-form-model-item>
          <a-form-model-item label="审核状态">
            <a-select style="width:150px" placeholder="请选择分类" v-model="doctorsIsverify">
              <a-select-option value="3">
                审核不通过
              </a-select-option>
              <a-select-option value="1">
                审核通过
              </a-select-option>
            </a-select>
          </a-form-model-item>
          <a-button type="primary" @click="search">
            查询
          </a-button>
        </a-col>
      </a-row>
    </a-form-model>
    <a-modal
      :visible="deletePatientInfoVisible"
      :confirm-loading="confirmLoading"
      @ok="handleOk2"
      @cancel="handleCancel2"
      width="350px"
    >
      <span>请确认是否要删除所选医生</span>
    </a-modal>

    <a-card :bordered="false" class="card-area">
      <a-table
        :pagination="ipagination"
        :loading="loading"
        rowKey="patientId"
        :row-selection="{ selectedRowKeys: selectedRowKeys, onChange: onSelectChange }"
        :columns="patientColumns"
        :data-source="doctors"
        :scroll="{ x: 1070 }"
        @change="handleTableChange"
      >
        <a slot="action" slot-scope="text, record">
          <a-button @click="handleAddPatient(record)">查看资料</a-button>
          <a-popconfirm
            v-if="record.doctorsIsverify == '冻结'"
            placement="topLeft"
            ok-text="确定"
            cancel-text="取消"
            @confirm="handleUnFrozen(record.dataId)"
          >
            <template slot="title">
              <p>是否确定解冻该账户？</p>
            </template>
            <a-button>解冻</a-button>
          </a-popconfirm>
          <a-popconfirm
            v-else
            placement="topLeft"
            ok-text="确定"
            cancel-text="取消"
            @confirm="handlefrozen(record.dataId)"
          >
            <template slot="title">
              <p>是否确定冻结该账户？</p>
            </template>
            <a-button>冻结</a-button>
          </a-popconfirm>
        </a>
      </a-table>
    </a-card>
    <team-doctor-modal ref="handleDoctor" :isEdit="isEdit" @cancel="cancel" @ok="handleSubmitOk"></team-doctor-modal>
  </div>
</template>

<script>
import { PAGE_MENU_TYPE } from '@/store/mutation-types'
import { getAction, postAction } from '@/api/manage'
import { axios } from '@/utils/request'
import signMd5Utils from '@/utils/encryption/signMd5Utils'
import JSelectPatientTag from '@/components/jeecgbiz/JSelectPatientTag'
import TeamDoctorModal from './drawer/TeamDoctorModal'
import moment from 'moment'
import { mapGetters } from 'vuex'

const patientColumns = [
  {
    title: '医生姓名',
    dataIndex: 'doctorsName',
    width: 100
  },
  {
    title: '手机号码',
    dataIndex: 'doctorsMobile',
    width: 150
  },
  {
    title: '医院',
    dataIndex: 'doctorsHospital',
    width: 120
  },
  // {
  //   title: '科室',
  //   dataIndex: 'doctorsDepartment',
  //   width: 120
  // },
  {
    title: '职称',
    dataIndex: 'doctorsLevel',
    width: 120
  },
  {
    title: '执业经验',
    dataIndex: 'doctorsTimeWork',
    width: 120
  },
  {
    title: '状态',
    dataIndex: 'doctorsIsverify',
    width: 120
  },
  {
    title: '操作',
    key: 'operation',
    width: 250,
    fixed: 'right',
    align: 'center',
    scopedSlots: { customRender: 'action' }
  }
]

export default {
  components: { JSelectPatientTag, TeamDoctorModal },
  data() {
    return {
      loading: true,
      /* 分页参数 */
      ipagination: {
        current: 1,
        pageSize: 10,
        pageSizeOptions: ['10', '20', '30'],
        showTotal: (total, range) => {
          return range[0] + '-' + range[1] + ' 共' + total + '条'
        },
        showQuickJumper: true,
        showSizeChanger: true,
        total: 0
      },
      doctorsName: '',
      doctorsMobile: '',
      doctorsIsverify: '',
      groupQueryUrl: '100/12/1001205',
      groupParams: {
        orgCode: 'A01',
        actId: 120802
      },
      formItemLayout: {
        labelCol: {
          xs: { span: 24 },
          sm: { span: 6 }
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 14 }
        }
      },
      doctorQueryUrl: '/201/02/2010213',
      doctorParams: {
        pageNum: 1,
        pageSize: 10,
        orgCode: this.orgCode()
      },
      activeKey: 0,
      patientColumns,
      doctors: [],
      result: [],
      selectedRowKeys: [],
      visible: false,
      flupPostData: {
        patientId: 0
      },
      batchIds: [],
      confirmLoading: false,
      isEdit: false,
      addParams: {},
      deletePatientInfoVisible: false,
      deleteDoctorUrl: '/201/02/2010203',
      modifyFrozen: '/201/02/2010211',
      modifyApprove: '/201/02/2010212'
    }
  },
  created() {
    this.getDoctors()
  },
  methods: {
    ...mapGetters(['doctorId', 'orgCode']),
    getDoctors() {
      this.selectedRowKeys = []
      this.loading = true
      postAction(this.doctorQueryUrl, this.doctorParams)
        .then(res => {
          if (res.success) {
            if (res.result.records) {
              this.result = res.result.records
              this.doctors = res.result.records.map(v => {
                if (v.doctorsStatus == 1) {
                  v.doctorsIsverify = '冻结'
                } else {
                  switch (v.doctorsIsverify) {
                    case 1:
                      v.doctorsIsverify = '审核通过'
                      break
                    case 3:
                      v.doctorsIsverify = '审核不通过'
                      break
                    default:
                      v.doctorsIsverify = '待审核'
                      break
                  }
                }
                // this.$refs.handleDoctor.orgList.forEach(n => {
                //   if (v.doctorsHospital == n.value) {
                //     v.doctorsHospital = n.title
                //   }
                // })
                this.$refs.handleDoctor.jobTitle.forEach(n => {
                  if (v.doctorsLevel == n.value) {
                    v.doctorsLevel = n.title
                  }
                })
                if (v.doctorsTimeWork) {
                  return {
                    ...v,
                    doctorsTimeWork: new Date().getFullYear() - v.doctorsTimeWork.substring(0, 4) + '年'
                  }
                } else {
                  return {
                    ...v,
                    doctorsTimeWork: '0年'
                  }
                }
              })
            }
            if (res.result.total) {
              this.ipagination.total = res.result.total
            } else {
              this.ipagination.total = 0
            }
          }
        })
        .finally(() => {
          this.loading = false
        })
    },
    search() {
      this.loading = true
      postAction(this.doctorQueryUrl, {
        pageNum: 1,
        pageSize: 10,
        doctorsName: this.doctorsName,
        doctorsMobile: this.doctorsMobile,
        doctorsIsverify: this.doctorsIsverify
      })
        .then(res => {
          if (res.success) {
            if (res.result.records) {
              this.result = res.result.records
              this.doctors = res.result.records.map(v => {
                if (v.doctorsStatus == 1) {
                  v.doctorsIsverify = '冻结'
                } else {
                  switch (v.doctorsIsverify) {
                    case 1:
                      v.doctorsIsverify = '审核通过'
                      break
                    case 3:
                      v.doctorsIsverify = '审核不通过'
                      break
                    default:
                      v.doctorsIsverify = '待审核'
                      break
                  }
                }
                if (v.doctorsTimeWork) {
                  return {
                    ...v,
                    doctorsTimeWork: new Date().getFullYear() - v.doctorsTimeWork.substring(0, 4) + '年'
                  }
                } else {
                  return {
                    ...v,
                    doctorsTimeWork: '0年'
                  }
                }
              })
            }
            if (res.result.total) {
              this.ipagination.total = res.result.total
            } else {
              this.ipagination.total = 0
            }
          }
        })
        .finally(() => {
          this.loading = false
        })
    },
    // getTag() {
    //   postAction(this.getTagUrl,{}).then((res) => {
    //     if (res.success) {
    //       this.tags = res.result
    //     }
    //   })
    // },
    handlefrozen(id) {
      postAction(this.modifyFrozen, { dataId: id, doctorsStatus: 1 }).then(res => {
        if (res.success) {
          this.$message.success(res.message)
        } else {
          this.$message.error(res.message)
        }
        this.getDoctors()
      })
    },
    handleUnFrozen(id) {
      postAction(this.modifyFrozen, { dataId: id, doctorsStatus: 0 }).then(res => {
        if (res.success) {
          this.$message.success(res.message)
        } else {
          this.$message.error(res.message)
        }
        this.getDoctors()
      })
    },
    onSelectChange(selectedRowKeys) {
      this.selectedRowKeys = selectedRowKeys
    },
    handleTableChange(pagination) {
      this.ipagination = pagination
      this.doctorParams.pageNum = pagination.current
      this.doctorParams.pageSize = pagination.pageSize
      this.getDoctors()
    },
    onChange(date, dateString) {
      this.flupPostData.flupDate = dateString
    },
    disabledDate(current) {
      return current < moment().subtract(1, 'day')
    },

    /**
     * 新增患者
     */
    handleAddPatient(doctor) {
      this.isEditDoctor = doctor && doctor.dataId
      this.doctorDialogTitle = this.isEditDoctor ? '医生基本信息' : '新增医生'
      if (this.isEditDoctor) {
        this.isEdit = true
        this.addParams = {
          id: doctor.dataId,
          doctorsMobile: doctor.doctorsMobile,
          doctorsIsverify: doctor.doctorsIsverify == '审核通过' ? 1 : doctor.doctorsIsverify == '审核不通过' ? 3 : 2
        }
        this.$refs.handleDoctor.edit(this.addParams)
        this.$refs.handleDoctor.title = this.doctorDialogTitle
      } else {
        this.addParams = {}
        this.$refs.handleDoctor.add(this.addParams)
        this.$refs.handleDoctor.title = this.doctorDialogTitle
      }
    },
    // 批量删除
    handleBatchDelete() {
      if (this.selectedRowKeys.length == 0) {
        this.$message.error('请选择医生')
      } else {
        this.deletePatientInfoVisible = true
      }
    },

    isRightMobileNumber(rule, value, callback) {
      if (typeof value === 'undefined' || value === '') {
        callback()
      } else {
        const regex = /^1[3456789]\d{9}$/
        if (!regex.test(value)) {
          this.isAnyWrongData = true
          callback(new Error('请输入正确手机格式'))
        } else {
          this.isAnyWrongData = false
        }
        callback()
      }
    },

    disabledDate1(current) {
      return current > moment().endOf('day')
    },

    onChange1(date, dateString) {
      this.addParams.patientBirth = dateString + ' 00:00:00'
    },

    handleSubmitOk() {
      this.getDoctors()
    },
    deleteAction1(url, parameter) {
      return axios({
        url: url,
        method: 'delete',
        data: parameter
      })
    },
    handleOk2(e) {
      this.confirmLoading = true
      const doctorIdList = []
      this.doctors.forEach((v, i) => {
        if (this.selectedRowKeys.includes(i)) {
          doctorIdList.push(v.dataId)
        }
      })
      const params = {
        doctorIds: [...doctorIdList]
      }
      console.log(params)
      this.deleteAction1(this.deleteDoctorUrl, params).then(res => {
        if (res.success) {
          this.$message.success(res.message)
          this.getDoctors()
          this.deletePatientInfoVisible = false
          this.confirmLoading = false
        } else {
          this.$message.error(res.message)
          this.confirmLoading = false
        }
      })
    },
    handleCancel2(e) {
      console.log('Clicked cancel button')
      this.deletePatientInfoVisible = false
    },
    cancel(isEdit) {
      console.log(isEdit, 'isedit')
      this.isEdit = isEdit
    }
  }
}
</script>

<style lang="less" scoped>
.ant-btn {
  margin: 0.2rem;
}

.flup-item {
  margin: 1rem;
  padding: 0.5rem;
  display: flex;
  align-items: center;

  span {
    flex: 1;
  }
}

#tip {
  display: block;

  #warning {
    // margin: 0.3rem;
    text-align: right;
  }
}

.it_search {
  background: white;
  border-top-left-radius: 2px;
  border-top-right-radius: 2px;
  padding: 10px 20px;
  color: rgba(0, 0, 0, 0.85);
}

.it_search > div {
  margin-bottom: 10px;
}

.ant-tag {
  font-size: 14px;
  border: 1px solid rgba(0, 0, 0, 0.65);
  margin-bottom: 5px;
}
.ant-tag-checkable-checked {
  border: 1px solid #f5222d;
}
/deep/.searchClass > .ant-form-item-control-wrapper {
  width: 150px;
}
</style>

